﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AccountNavigationViewModel.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the <seealso cref="AccountNavigationViewModel" /> type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Client.Modules.Core.Account.ViewModel
{
    using System;
    using System.Collections.ObjectModel;
    using System.Diagnostics.Contracts;
    using System.Globalization;
    using System.Linq;
    using System.Windows.Input;
    using System.Windows.Media.Imaging;
    using Business.Interfaces;
    using Common;
    using Common.BaseClasses;
    using Common.Services;
    using Hisaab.Common;
    using Microsoft.Practices.Prism;
    using Microsoft.Practices.Prism.Commands;
    using Microsoft.Practices.Prism.Regions;
    using Microsoft.Practices.ServiceLocation;
    using Model;
    using Transaction;

    /// <summary>
    /// Defines the <seealso cref="AccountNavigationViewModel"/> type.
    /// </summary>
    public class AccountNavigationViewModel : NavigationViewModel
    {
        /// <summary>
        /// Session manager
        /// </summary>
        private readonly ISession _session;

        /// <summary>
        /// Account object
        /// </summary>
        private readonly IAccountManager _accountManager;

        /// <summary>
        /// Accounts list
        /// </summary>
        private ObservableCollection<Account> _accounts;

        /// <summary>
        /// Selected account
        /// </summary>
        private Account _selectedAccount;

        /// <summary>
        /// Member variable _refreshCommand
        /// </summary>
        private ICommand _refreshCommand;

        /// <summary>
        /// Initializes a new instance of the <see cref="AccountNavigationViewModel"/> class.
        /// </summary>
        public AccountNavigationViewModel()
            : this(ServiceLocator.Current.GetInstance<IAccountManager>(), ServiceLocator.Current.GetInstance<ISession>())
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AccountNavigationViewModel"/> class.
        /// </summary>
        /// <param name="accountManager">The account.</param>
        /// <param name="session">The session</param>
        public AccountNavigationViewModel(IAccountManager accountManager, ISession session)
        {
            Contract.Requires<ArgumentNullException>(accountManager != null);
            Contract.Requires<ArgumentNullException>(session != null);

            _accountManager = accountManager;
            _session = session;
            Title = "Accounts";
            Image = new BitmapImage(
                        new Uri(
                        "Pack://application:,,,/Hisaab.Client.Common;component/Resources/Images/32/Account.png",
                        UriKind.Absolute));

            ImageSmall =
                        new BitmapImage(
                        new Uri(
                        "Pack://application:,,,/Hisaab.Client.Common;component/Resources/Images/16/Account.png",
                        UriKind.Absolute));

            GlobalCommands.Refresh.RegisterCommand(RefreshCommand);
            OnRefresh();
        }

        /// <summary>
        /// Gets RefreshCommand.
        /// </summary>
        public ICommand RefreshCommand
        {
            get
            {
                if (_refreshCommand == null)
                {
                    _refreshCommand = new DelegateCommand(OnRefresh);
                }

                return _refreshCommand;
            }
        }

        /// <summary>
        /// Gets the accounts.
        /// </summary>
        public ObservableCollection<Account> Accounts
        {
            get
            {
                return _accounts;
            }

            private set
            {
                if (value != _accounts)
                {
                    _accounts = value;

                    RaisePropertyChangedEvent("Accounts");
                }
            }
        }

        /// <summary>
        /// Gets or sets the selected account.
        /// </summary>
        /// <value>
        /// The selected account.
        /// </value>
        public Account SelectedAccount
        {
            get
            {
                return _selectedAccount;
            }

            set
            {
                if (value != _selectedAccount)
                {
                    _selectedAccount = value;
                    RaisePropertyChangedEvent("SelectedAccount");
                    OnAccountSelected();
                }
            }
        }

        /// <summary>
        /// Called when account selected.
        /// </summary>
        public void OnAccountSelected()
        {
            if (SelectedAccount != null)
            {
                // Initialize
                var regionManager = ServiceLocator.Current.GetInstance<IRegionManager>();

                // Show Workspace
                var query = new UriQuery { { "ID", SelectedAccount.AccountId.ToString(CultureInfo.InvariantCulture) } };
                var dashboardUri = new Uri(TransactionModule.TransactionsName + query, UriKind.RelativeOrAbsolute);
                regionManager.RequestNavigate(Regions.WorkspaceRegion, dashboardUri);

                LastNavigatedUri = dashboardUri;
            }
        }

        /// <summary>
        /// Called when [refresh].
        /// </summary>
        private void OnRefresh()
        {
            this.Accounts =
                new ObservableCollection<Account>(
                    this._accountManager.List(new Business.Entities.Account { UserId = this._session.LoggedUser }).Select(
                        x => x.Map<Business.Entities.Account, Account>()));
        }
    }
}